<?php

class DataGrabber_Armory 
{
    protected $server = "http://eu.wowarmory.com"; 
    protected $cache = null;
    
    // Some values for interfacing with the armory
    const _timeout = 30; // Timeout for armory request
    const _useragent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
    
    public function __construct($cacheFolder = null, $cacheLifetime = null)
    {
        $config = Zend_Registry::getInstance()->configuration;
        
        if (!isset($config->armory->server)) {
            throw new Exception('Armory server not defined. Please do so under: armory.server');
        }
        
        $this->server = $config->armory->server;
        
        // Use the default armory folder if nothing else has been specified
        if (!$cacheFolder) {
            
            $config = Zend_Registry::getInstance()->configuration;
            $armoryFolder = isset($config->datagrabber->folders->armory) ? $config->datagrabber->folders->armory : null;
            
            if (!$armoryFolder) {
                throw new Exception('Armory folder is not defined in config file. Please do so under: datagrabber.folder.wowhead');
            } else if (!is_writable($armoryFolder)) {
                throw new Exception('Armory folder: ' . $armoryFolder . ' is not writeable. ');
            }
            
            $cacheFolder = $armoryFolder;
        }
        
        $this->cache = new DataGrabber_Cache($cacheFolder, $cacheLifetime);
    }
    
    public function fetchDungeons()
    {
        return $this->fetch('data/dungeons.xml');
    }
    
    public function fetchDungeonStrings() 
    {
        return $this->fetch('data/dungeonStrings.xml');
    }
    
    public function fetchLootable($bossId, $heroic = false, $testCache = false)
    {
        $resource = 'search.xml?fl[source]=dungeon&fl[boss]=' . $bossId . '&fl[difficulty]=' . (($heroic)?'heroic':'normal') . '&searchType=items';
        return $this->fetch($resource, $testCache);
    }
    
    public function fetch($resource, $testCache = false) 
    {  
        $url = $this->server . '/' . $resource;
       
        // Attempt to get a cached version first
        $contents = $this->cache->get($url);  
        if ($contents) {
            if ($testCache) {
                return true;
            }
            return $contents;
        }
        
        if ($testCache) {
            return false;
        }
        
        try {
            $contents = $this->performRequest($url);
        } catch(Exception $e) {
            // Handle something 
            throw $e;
        }
        
        $this->cache->put($url, $contents);
        return $contents;
    }
    
    protected static function performRequest($url)
    {  
        $client = new Zend_Http_Client($url);
        $client->setConfig(array('useragent'=>self::_useragent, 'timeout'=>self::_timeout));
        $response = $client->request();
        
        if ($response->isSuccessful()) {
            return $response->getBody();
        } 
        
        throw new Exception("Could not retrive: '" . $url . "'");
    }
}

?>